<?php
class StuffAction extends CommonAction {

    public function index()
    {
        $stuff = M('Stuff');
        parent::page($stuff->count());
        $data = $stuff->order('add_time desc')
                     ->limit($this->limit)
                     ->select();

        $this->assign('data', $data);
        $this->display();
    }

    public function add()
    {
        $stuff = M('Stuff');
        if(isset($_POST['send']))
        {
            $this->validate(self::$ADD_SEC);

            $data['stuff_name'] = $this->_post('stuff_name');
            $data['stuff_type'] = $this->_post('stuff_type');
            $data['stuff_unit'] = $this->_post('stuff_unit');
            $data['stuff_price'] = $this->_post('stuff_price');
            $data['add_user'] = session('admin');
            $data['add_time'] = getDateTime();

            if($stuff->add($data))
            {
                $content = "用户：".session('admin')." 添加了一个菜品材料 名称：$data[stuff_name] 种类：$data[stuff_type] 单位： $data[stuff_unit] 单价： $data[stuff_price]";
                addAdminRecord($content);
                redirectByModal('添加菜品材料成功!', self::$SUCCESS, __URL__ . '/index');
            }
        }
        $this->display();
    }

    public function update($id='')
    {
        $stuff = M('Stuff');
        $currentData = $stuff->where('id='.$id)
                             ->find();

        if(isset($_POST['send']))
        {
            $this->validate(self::$UPDATE_SEC);

            $data['stuff_name'] = $this->_post('stuff_name');
            $data['stuff_unit'] = $this->_post('stuff_unit');
            $data['stuff_type'] = $this->_post('stuff_type');
            $data['stuff_price'] = $this->_post('stuff_price');
            $data['id'] = $this->_post('id');
            $content = "用户：".session('admin')." 修改了一个菜品材料  名称为：$currentData[stuff_name]";

            $stuff->save($data);

            addAdminRecord($content);
            redirectByModal('修改材料成功!', self::$SUCCESS, __URL__.'/index');
        }
        $this->assign('data', $currentData);
        $this->display();
    }

    public function delete()
    {
        $id = $this->_post('id');
        $stuff = M('Stuff');
        $data = $stuff->field(array('stuff_name', 'stuff_unit', 'stuff_type'))->where('id='.$id)->find();

        if($stuff->where('id='.$id)->delete())
        {
            $content = "用户：".session('admin')." 删除了一个菜品材料 名称：$data[stuff_name] 种类：$data[stuff_type] 单位： $data[stuff_unit] 单价：$data[stuff_price]";
            addAdminRecord($content);
            redirectByModal('删除材料成功!', self::$SUCCESS, __URL__ . '/index');
        }
    }

    // 验证
    private function validate($sec)
    {
        $stuff_name = $this->_post('stuff_name');
        $stuff_type = $this->_post('stuff_type');
        $stuff_unit = $this->_post('stuff_unit');
        $stuff_price = $this->_post('stuff_price');

        if($stuff_name == '')
            redirectByModal('材料名称必须填写!', self::$ERROR);
        else if(mb_strlen($stuff_name, 'utf-8') > 15)
            redirectByModal('材料名称不能超过15位!', self::$ERROR);
        else if($stuff_type == -1)
            redirectByModal('材料种类必须选择!', self::$ERROR);
        else if($stuff_unit == '')
            redirectByModal('材料单位必须选择!', self::$ERROR);
        else if(mb_strlen($stuff_unit, 'utf-8') > 4)
            redirectByModal('材料单位不能超过四位!', self::$ERROR);
        else if($stuff_price == '')
            redirectByModal('材料单价必须填写!', self::$ERROR);
        else if(!is_numeric($stuff_price))
            redirectByModal('材料单价必须是数字!', self::$ERROR);
        else if($sec == 'add')
        {
            $stuff = M('Stuff');
            if($stuff->where("stuff_name='$stuff_name'")->find())
                redirectByModal('材料名称已经存在!', self::$ERROR);
        }
    }
}